#include "Server/include/Request.h"


void RequestHead::setMethod(std::string& method){
    this->method=method;
}
void RequestHead::setUrl(std::string& url){
    this->url=url;
}

void RequestHead::setVersion(std::string& version){
    this->version=version;
}
void RequestHead::setMsg(std::string& key,std::string& value){
    this->msg.insert(std::pair<std::string,std::string>(key,value));
}

void RequestHead::clear(){
    this->msg.clear();
}

int add(const char* buf,int buf_size,std::shared_ptr<RequestHead>& RHead){
    std::string str=buf;
    std::istringstream stream(str);
    // // std::getline(s,str);
    std::string ss;
    std::getline(stream,ss);
    if(!(ss.size()>2 && ss[ss.size()-1]=='\r')){
        return -1;
    }
    {
        std::istringstream stream2(ss);
        std::string s1;
        std::vector<std::string> box;
        s1.erase(s1.end()-1);
        while(stream2>>s1){
            box.push_back(s1);
        }
        if(box.size()!=3){
            return -1;
        }
        RHead->setMethod(box[0]);
        RHead->setUrl(box[1]);
        RHead->setVersion(box[2]);
    }
    RHead->clear();
    while(std::getline(stream,ss) && ss[ss.size()-1]=='\r'){
        ss.erase(ss.end()-1);
        std::string::size_type end=ss.find(": ",0);
        if (end != std::string::npos)
			{
				std::string key = ss.substr(0, end);
				std::string value = ss.substr(end + 2);
				RHead->setMsg(key,value);
			}
    }
    return 0;

}
